package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.domain.Involved;
import com.example.evanproject.domain.Register;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.service.InvolvedService;
import com.example.evanproject.mapper.InvolvedMapper;
import com.example.evanproject.util.EntityMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author hanxiaoqi
* @description 针对表【involved】的数据库操作Service实现
* @createDate 2024-03-09 21:52:13
*/
@Service
public class InvolvedServiceImpl extends ServiceImpl<InvolvedMapper, Involved>
    implements InvolvedService{


    @Autowired
    private InvolvedMapper involvedMapper;
    @Autowired
    private RegisterMapper registerMapper;

    /*返回某个用户的关注的与粉丝的人数以及具体信息(头像以及姓名)*/
    public Map selectbyidinfo(Integer userid) {
        Map<String,Object> retrunmap=new HashMap<>();
//        返回我的关注
        List<Involved> list1=involvedMapper.selectfollowedbyid(userid);
        List<Map> list3=new ArrayList<>();
        for(int i=0;i<list1.size();i++){
            Map<String,Object> map1= EntityMapUtils.entityToMap(list1.get(i));
            map1.put("people",trunlist(list1.get(i).getBefollowedId()));
            list3.add(map1);
        }
        retrunmap.put("myfollow",list3);

        //返回我的粉丝
        List<Involved> list6=involvedMapper.selectfansbyid(userid);
        List<Map> list4=new ArrayList<>();
        for(int i=0;i<list6.size();i++){
            Map<String,Object> map2= EntityMapUtils.entityToMap(list6.get(i));
            map2.put("people",trunlist(list6.get(i).getFollowersId()));
            list4.add(map2);
        }
        retrunmap.put("myfans",list4);

        return retrunmap;
    }


    //    整合消息(将用户信息放进去里面)
    public Map trunlist(Integer id){
        Register register= registerMapper.selectById(id);
        Map<String,Object> maplist=new HashMap<>();
        maplist.put("name",register.getSName());
        maplist.put("userid",register.getSId());
        maplist.put("img",register.getSImg());
        maplist.put("sign",register.getSSign());
        maplist.put("age",register.getSAge());
        return maplist;
    }

    //    添加关注以及取消关注(flag==1表示添加id,flag==2表示取消关注)
    public Integer trunstateinfo(Integer myId, Integer userId, Integer flag) {
        if(flag==1){
            return involvedMapper.addfollow(myId,userId);
        }
        else {
            return involvedMapper.deletefollow(myId,userId);
        }



    }

    public List relationship(Integer followersId, Integer befollowedId) {
        List<String> list=involvedMapper.relationship(followersId,befollowedId);
        return list;
    }

    public Map relationListNum(Integer peopleId) {
        return involvedMapper.relationListNum(peopleId);
    }
}




